package com.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.reggie.common.R;
import com.reggie.dto.OrderDto;
import com.reggie.entity.OrderDetail;
import com.reggie.entity.Orders;
import com.reggie.service.OrdersService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/*
订单
 */
@RestController
@RequestMapping("/order")
@Slf4j
public class OrderController {
    @Autowired
    private OrdersService ordersService;

//    提交订单
    @PostMapping("/submit")
    public R<String> submit(@RequestBody Orders orders){
//        order表中需要用户表和地址表中的数据
        ordersService.submit(orders);

        return R.success("下单成功");
    }

//    后台订单分页
    @GetMapping("/page")
    public R<Page> page(int page,int pageSize,Long number,String beginTime,String endTime){
        Page<Orders> pageInfo=new Page<>(page,pageSize);

        LambdaQueryWrapper<Orders> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.like(number!=null,Orders::getNumber,number);
       queryWrapper.orderByDesc(Orders::getOrderTime)
                   .gt(StringUtils.isNotBlank(beginTime),Orders::getOrderTime,beginTime)
                   .lt(StringUtils.isNotBlank(endTime),Orders::getOrderTime,endTime);

       ordersService.page(pageInfo,queryWrapper);
       return R.success(pageInfo);

    }

//    更改状态
    @PutMapping
    public R<String> status(@RequestBody Orders orders){
        ordersService.updateById(orders);
        return R.success("修改状态成功");
    }

    @GetMapping("/userPage")
    public R<Page> userPage(int page, int pageSize){
        Page<Orders> pageInfo=new Page<>(page,pageSize);
        Page<OrderDto> pageDto=new Page<>(page,pageSize);
        LambdaQueryWrapper<Orders> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.orderByDesc(Orders::getOrderTime);
        ordersService.page(pageInfo,queryWrapper);

        BeanUtils.copyProperties(pageInfo,pageDto,"records");

        List<Orders> orders = pageInfo.getRecords();
        List<OrderDto> orderDtoList=orders.stream().map((item)->{
            OrderDto orderDto=new OrderDto();
            Long id = item.getId();
            List<OrderDetail> detailList = ordersService.getOrderDetailByOrderNumber(id);
            orderDto.setOrderDetails(detailList);
            BeanUtils.copyProperties(item,orderDto);

            return orderDto;

        }).collect(Collectors.toList());

        pageDto.setRecords(orderDtoList);
        return R.success(pageDto);
    }

    @PostMapping("/again")
    public R<String> again(@RequestBody Map map){
        String string = map.get("id").toString();
        Long id=Long.parseLong(string);
        ordersService.again(id);

        return R.success("下单成功");
    }
}
